/**
 * Created by John on 2022/10/9.
 */

public with sharing class TestJQueryController {

    private List<FieldSetMemberVO> filterFieldSets = new List<FieldSetMemberVO>();

    private List<FieldSetMemberVO> fieldSets = new List<FieldSetMemberVO>();

    private List<Student__c> studentList = new List<Student__c>();

    public List<Student__c> getStudentList() {
        return this.studentList;
    }

    public TestJQueryController() {
        Schema.DescribeSObjectResult describe = Student__c.SObjectType.getDescribe();
        Map<String, FieldSet> fieldSetMap = describe.fieldSets.getMap();
        FieldSet conditionFieldSet = fieldSetMap.get('Condition_FieldSet');
        FieldSet listFieldSet = fieldSetMap.get('List_FieldsSet');

        for(FieldSetMember fsm : conditionFieldSet.getFields()) {
            filterFieldSets.add(new FieldSetMemberVO(fsm.getFieldPath(), fsm.getType().name(), fsm.getLabel(), fsm.getRequired()));
        }

        String selectFields = '';
        for(FieldSetMember fsm : listFieldSet.getFields()) {
            fieldSets.add(new FieldSetMemberVO(fsm.getFieldPath(), fsm.getType().name(), fsm.getLabel(), fsm.getRequired()));
            selectFields += fsm.getFieldPath()+',';
        }
        selectFields = selectFields.substringBeforeLast(',');

        studentList = Database.query('SELECT ' + selectFields + ' FROM Student__c');
    }

    public List<FieldSetMemberVO> getFilterFieldSets() {
        return filterFieldSets;
    }

    public List<FieldSetMemberVO> getFieldSets() {
        return fieldSets;
    }

    public class FieldSetMemberVO {
        public String name {set; get;}
        public String type {set; get;}
        public String label {set; get;}
        public Boolean required {set; get;}

        public FieldSetMemberVO(){}

        public FieldSetMemberVO(String name, String type, String label, Boolean required) {
            this.name = name;
            this.type = type;
            this.label = label;
            this.required = required;
        }
    }
}